package com.ruoyi.movie.controller;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.movie.domain.MovieBasics;
import com.ruoyi.movie.domain.Video;
import com.ruoyi.movie.service.IMovieBasicsService;
import com.ruoyi.movie.service.IVideoService;

/**
 * 用户端影视接口
 *
 * @author ruoyi
 * @date 2025-10-21
 */
@RestController
@RequestMapping("/app/movie")
public class AppMovieController extends BaseController
{
    @Autowired
    private IMovieBasicsService movieBasicsService;

    @Autowired
    private IVideoService videoService;

    /**
     * 获取影视列表（前台）
     */
    @Anonymous
    @GetMapping("/list")
    public TableDataInfo list(MovieBasics movieBasics)
    {
        movieBasics.setStatus("0"); // 只显示正常状态
        startPage();
        List<MovieBasics> list = movieBasicsService.selectMovieBasicsList(movieBasics);
        return getDataTable(list);
    }

    /**
     * 获取影视详情（前台）
     */
    @Anonymous
    @GetMapping("/detail/{movieBasicsId}")
    public AjaxResult getDetail(@PathVariable("movieBasicsId") Integer movieBasicsId)
    {
        MovieBasics movie = movieBasicsService.selectMovieBasicsById(movieBasicsId);
        if (movie == null) {
            return AjaxResult.error("影视不存在");
        }

        // TODO: 增加访问量需要通过MoviePv表实现
        // movieBasicsService.increaseHits(movieBasicsId);

        // TODO: Video表在新设计中不再有movie_id字段
        // 需要重新设计视频与电影的关联方式，可能需要通过中间表或其他方式关联

        Map<String, Object> result = new HashMap<>();
        result.put("movie", movie);
        // result.put("videos", videos); // 暂时移除视频列表

        return AjaxResult.success(result);
    }

    /**
     * 获取推荐影视
     */
    @Anonymous
    @GetMapping("/recommend")
    public AjaxResult getRecommend()
    {
        MovieBasics movieBasics = new MovieBasics();
        // TODO: 推荐功能需要通过MovieRate评分表实现
        // movieBasics.setIsRecommend("1");
        movieBasics.setStatus("0");
        List<MovieBasics> list = movieBasicsService.selectMovieBasicsList(movieBasics);
        return AjaxResult.success(list);
    }

    /**
     * 获取热门影视
     */
    @Anonymous
    @GetMapping("/hot")
    public AjaxResult getHot()
    {
        MovieBasics movieBasics = new MovieBasics();
        // TODO: 热门功能需要通过MoviePv访问量表实现
        // movieBasics.setIsHot("1");
        movieBasics.setStatus("0");
        List<MovieBasics> list = movieBasicsService.selectMovieBasicsList(movieBasics);
        return AjaxResult.success(list);
    }

    /**
     * 获取最新影视
     */
    @Anonymous
    @GetMapping("/latest")
    public AjaxResult getLatest()
    {
        MovieBasics movieBasics = new MovieBasics();
        // TODO: 最新功能需要通过releaseDate或createTime排序实现
        // movieBasics.setIsNew("1");
        movieBasics.setStatus("0");
        List<MovieBasics> list = movieBasicsService.selectMovieBasicsList(movieBasics);
        return AjaxResult.success(list);
    }

    /**
     * 搜索影视
     */
    @Anonymous
    @GetMapping("/search")
    public TableDataInfo search(@RequestParam String keyword)
    {
        MovieBasics movieBasics = new MovieBasics();
        movieBasics.setTitle(keyword);
        movieBasics.setStatus("0");
        startPage();
        List<MovieBasics> list = movieBasicsService.selectMovieBasicsList(movieBasics);
        return getDataTable(list);
    }

    /**
     * 按类型获取影视
     */
    @Anonymous
    @GetMapping("/type/{columnValue}")
    public TableDataInfo getByType(@PathVariable String columnValue)
    {
        MovieBasics movieBasics = new MovieBasics();
        movieBasics.setColumnValue(columnValue);
        movieBasics.setStatus("0");
        startPage();
        List<MovieBasics> list = movieBasicsService.selectMovieBasicsList(movieBasics);
        return getDataTable(list);
    }

    /**
     * 获取排行榜
     */
    @Anonymous
    @GetMapping("/ranking/{type}")
    public AjaxResult getRanking(@PathVariable String type)
    {
        // TODO: 排行榜功能需要通过MoviePv和MovieRate表实现
        // List<MovieBasics> list = null;
        // switch(type) {
        //     case "day":
        //         list = movieBasicsService.getDayRanking();
        //         break;
        //     case "week":
        //         list = movieBasicsService.getWeekRanking();
        //         break;
        //     case "month":
        //         list = movieBasicsService.getMonthRanking();
        //         break;
        //     default:
        //         list = movieBasicsService.getYearRanking();
        // }
        // return AjaxResult.success(list);

        // 临时返回空列表
        MovieBasics movieBasics = new MovieBasics();
        movieBasics.setStatus("0");
        List<MovieBasics> list = movieBasicsService.selectMovieBasicsList(movieBasics);
        return AjaxResult.success(list);
    }
}